<template>
  <div class="content">
    <div class="common-seach-wrap" :style="{ marginBottom: '10px' }">
      <el-button type="primary" icon="Plus" @click="handleOpen('add', { type: '使用问题' })"
        >添加问题</el-button
      >
    </div>
    <!--搜索表单-->
    <el-table :data="tableData" row-key="problem_id" height="100%" stripe :border="true">
      <el-table-column prop="problem_id" label="ID" fixed="left" width="50"></el-table-column>
      <el-table-column prop="title" label="问题名称" />
      <el-table-column prop="description" label="问题描述" />
      <el-table-column label="展示状态">
        <template #default="scope">
          <el-radio-group v-model="scope.row.is_show" @change="setShow(scope.row)">
            <el-radio
              v-for="item in [
                { label: '打开', value: 1 },
                { label: '关闭', value: 0 },
              ]"
              :key="item.value"
              :label="item.value"
            >
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="300">
        <template #default="scope">
          <el-button type="primary" link icon="Edit" @click="handleOpen('edit', scope.row)">
            编辑
          </el-button>
          <el-button type="danger" link icon="DeleteFilled" @click="delInfo(scope.row.problem_id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider border-style="none" />
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <Drawer ref="infoDrawer" @doneSave="getTableInfo" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import Drawer from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox, TabPaneName } from "element-plus";
import { deleteProblemApi, problemListApi, updateProblemApi } from "@/api/babycar";
const total = ref(0);
const curPage = ref(1);
const pageSize = ref(20);
const departmentOption = ref<any[]>([]);
const tableData = ref([]);
const infoDrawer = ref<any>(null);
onMounted(() => {
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
function getTableInfo() {
  problemListApi({
    page: curPage.value,
    page_size: pageSize.value,
    type: "使用问题",
  }).then((res) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除该问题吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteProblemApi(id).then((res: any) => {
        getTableInfo();
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

const setShow = (row: any) => {
  updateProblemApi(row).then((res: any) => {
    if (res.code == 1) {
      ElMessage({
        type: "success",
        message: "状态更新成功！",
      });
    }
    getTableInfo();
  });
};

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(type: string, row?: any) {
  infoDrawer.value.open(type, row);
}
defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
